package com.learning.mediacomponent;

import static com.learning.mediacomponent.RecordActivity.uri_list;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;

import java.io.IOException;
import java.util.jar.JarException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        Button recordTestButton = findViewById(R.id.btn_record_test);
        recordTestButton.setOnClickListener(this);
        Button playTestButton = findViewById(R.id.btn_play_test);
        playTestButton.setOnClickListener(this);
        Button videoTestButton = findViewById(R.id.btn_video);
        videoTestButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_record_test) {
            // Android 13+
             {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                        || ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this,
                            new String[]{
                                    Manifest.permission.READ_EXTERNAL_STORAGE,
                                    Manifest.permission.RECORD_AUDIO,
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    android.Manifest.permission.READ_PHONE_STATE}, 1);
                } else {
                    Intent intent = new Intent(MainActivity.this,RecordActivity.class);
                    startActivity(intent);
                }
            }
        } else if (v.getId() == R.id.btn_play_test) {
            // Android 13+
             {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 2);
                } else {
                    Intent intent = new Intent(MainActivity.this,PlayingRecordActivity.class);
                    startActivity(intent);
                }
            }
        } else if (v.getId() == R.id.btn_video) {
            // Android 13+
            {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 3);
                } else {
                    Intent intent = new Intent(MainActivity.this,VideoActivity.class);
                    startActivity(intent);
                }
            }


        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户授予了权限
                Toast.makeText(this, "yes", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,RecordActivity.class);
                startActivity(intent);
            } else {
                // 用户拒绝了权限，显示提示
                Toast.makeText(this, "永久拒绝了权限，请前往系统设置开启权限", Toast.LENGTH_SHORT).show();
            }
        } else if (requestCode == 2) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户授予了权限
                Toast.makeText(this, "yes", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,PlayingRecordActivity.class);
                startActivity(intent);
            } else {
                // 用户拒绝了权限，显示提示
                Toast.makeText(this, "永久拒绝了权限，请前往系统设置开启权限", Toast.LENGTH_SHORT).show();
            }
        } else if (requestCode == 3) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户授予了权限
                Toast.makeText(this, "yes", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,VideoActivity.class);
                startActivity(intent);
            } else {
                // 用户拒绝了权限，显示提示
                Toast.makeText(this, "永久拒绝了权限，请前往系统设置开启权限", Toast.LENGTH_SHORT).show();
            }
        }
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        JSONObject resultJSON = new JSONObject();
        try {
            resultJSON.put("code", "0000");
            resultJSON.put("msg", "录音成功");
            JSONObject toDoJSON = new JSONObject();
            JSONArray uriArray = new JSONArray(uri_list);
            toDoJSON.put("URL",uriArray);
            resultJSON.put("result", toDoJSON);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }

        Log.i("输出所有保存的音频uri", "onRestart: " + uri_list.toString());
        Log.i("输出所有保存的音频uri", "onRestart: " + resultJSON.toString());
        System.out.println(resultJSON.toString());
    }
}